package com.ladlee.parking.config;

import cn.hutool.core.date.DateUtil;
import com.ladlee.parking.sequence.builder.DbSeqBuilder;
import com.ladlee.parking.sequence.properties.SequenceDbProperties;
import com.ladlee.parking.sequence.seq.Sequence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class SequenceConfig {

    /**
     * 订单流水号发号器
     *
     * @param dataSource 数据源配置
     * @param properties 参数配置
     * @return
     */
    @Bean
    public Sequence paySequence(DataSource dataSource, SequenceDbProperties properties) {
        return DbSeqBuilder
                .create()
                .bizName(() -> String.format("pay_%s", DateUtil.today()))
                .dataSource(dataSource)
                .step(properties.getStep())
                .retryTimes(properties.getRetryTimes())
                .tableName(properties.getTableName())
                .build();
    }
}
